#!user/bin/env python3
# -- coding: utf-8 --
# @Createtime：2023/6/17
# @Updatetime：12:24
# @Author：Test008
# @File：func.py
# @Description：python高级特性

# 递归函数
def fact(n):
    if n == 1:
        return 1
    else:
        return n * fact(n - 1)


# print(fact(3))
# print(fact(10))

# 切片
L = ['mack', 'jack', 'Bob', 'Track']
# print(L[:3])

# K=list(range(100))
# K=tuple(range(100))
K = 'tuplerange(100)'
# print(K)
# print(K[:10])
# print(K[-10:])
# print(K[10:21])
# print(K[:])
# print(K[:3])
# print(K[-5:-1])

# 迭代
# 如何判断一个对象是可迭代对象呢？方法是通过collections模块的Iterable类型判断：
from collections.abc import Iterable

# print(isinstance('abc', Iterable))
# for i in 'abs':
# print(i)
# Python内置的enumerate函数可以把一个list变成索引-元素对
# for i, value in enumerate(['A', 'B', 'C']):
#     print(i, value)

# for x, y in [(1, 1), (2, 4), (3, 9)]:
#     print(x, y)

# 列表生成式
li = list(range(1, 11))
# print(li)
l = []
for x in range(1, 11):
    l.append(x * x)

# print(l)
# 列表推导式
ra = [x * x for x in range(1, 11)]
# print(ra)
L1 = [x * x for x in range(1, 11) if x % 2 == 0]
# print(L1)
L2 = [x + y for x in 'ABC' for y in 'XYZ']
# print(L2)
L3 = [x + y + z for x in 'ABC' for y in 'XYZ' for z in '123']
# print(len(L3),L3)
# 列出当前目录下的所有文件和目录名，可以通过一行代码实现：
import os

Os = [d for d in os.listdir('.')]
# print(Os)
# for循环其实可以同时使用两个甚至多个变量，比如dict的items()可以同时迭代key和value：
d = {'a': 'A', 'b': 'B', 'c': 'C'}
# for k,v in d.items():
#     print(k,'=',v)
#
Dd = [k + '=' + v for k, v in d.items()]
# print(Dd)
# 最后把一个list中所有的字符串变成小写
L4 = ['Hello', 'World', 'IBM', 'Apple']
Lo = [l.lower() for l in L4]
# print(Lo)
# 生成器
g = (x for x in range(10))


# print(g)
# print(next(g))
# print(next(g))
def re(start,end):
    for n in g:
        if n % 2 == 0:
            yield n
y=re(0,30)
# print(type(y))
# print(y)
# print(next(y))
# print(next(y))
# print(next(y))
# print(next(y))
# for i in y:
#     print(i)

# 迭代器
print(isinstance('ins',Iterable))
print(isinstance((),Iterable))
print(isinstance({},Iterable))
print(isinstance([],Iterable))
print(isinstance(98,Iterable))
# print(isinstance(张三,Iterable))
